/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  cmap_sort.c
 *  scatterd
 *
 *  Created by Grant Jones on 3/10/08.
 *
 */

#include "cmap_sort.h"
#include <stdlib.h>

typedef struct
{
	int (*compar)(void *, const void *, const void *);
	void *ctx;
#ifdef SORT_CALL_COUNT
	int debug_call_count;
#endif	
} cmapCompWrapStruct;

/* wrapper function, hides the internal cmap structure from the external compare function */
static int cmap_sortbyvalue_comp( void *ctx, const void *a, const void *b )
{
	cmapCompWrapStruct *cws = (cmapCompWrapStruct *)ctx;
#ifdef SORT_CALL_COUNT
	cws->debug_call_count++;
#endif
	return cws->compar( cws->ctx, *((void **)(a+sizeof(CMAP_KEY_TYPE))), *((void **)(b+sizeof(CMAP_KEY_TYPE))) );
}

#ifdef NO_QSORT_R
/* sigh, this is NOT threadsafe but provided for systems that don't have a qsort_r function *cough*Linux*cough* */
typedef int (*cmap_qsort_cb)(void *, const void *, const void *) ;
static void *g_edb_thunk = NULL;
static cmap_qsort_cb g_edb_qsort_cb = NULL;


static int cmap_sort_qsort_cb( const void *a, const void *b )
{
	return g_edb_qsort_cb( g_edb_thunk, a, b );
}
//void qsort_r(void *base, size_t nel, size_t width, void *thunk, int (*compar)(void *, const void *, const void *))
void linux_qsort_r(void *base, size_t nel, size_t width, void *thunk, cmap_qsort_cb compar )
{
	assert(compar != NULL);
	
	g_edb_thunk = thunk;
	g_edb_qsort_cb = compar;
	qsort(base, nel, width, cmap_sort_qsort_cb);
}

#define qsort_r linux_qsort_r

#endif

void cmap_sortbyvalue( cmapStruct *cmap, void *ctx, int (*compar)(void *, const void *, const void *) )
{
	cmapCompWrapStruct cws;
	CMAP_PTR *pairptr;
	int z;
	cws.compar = compar;
	cws.ctx = ctx;
#ifdef SORT_CALL_COUNT
	cws.debug_call_count = 0;
#endif	
	qsort_r(cmap->kd, cmap->num, CMAP_PAIR_SZ, &cws, cmap_sortbyvalue_comp );

#ifdef SORT_CALL_COUNT
	edb_log_printf(0, "%s : call count: %d\n", __FUNCTION__, cws.debug_call_count );
#endif
	/* it is necessary to do key reassigning to maintain the required key order of the cmap */
	
	for( z = 0; z < cmap->num; z ++ )
	{
		pairptr = cmap->kd + ( CMAP_PAIR_SZ * z );
		*((int *)pairptr) = z+1;
	}
}



